package yxy.game.pm2.active.command.bottlewish;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actwish;
import yxy.cherry.data.bean.actwishdata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心愿瓶;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BottleWishMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/****************************************
 * @name CMD_Active_心愿瓶_BottleWishInfo
 * @description 心愿瓶活动许愿页面信息
 * @author 梁健彬
 * @since 2022-08-22
 ***************************************/
@MessageType(proto = Command.BottleWishInfo, classtype = BottleWishMessage.BottleWishInfoRequest.class)
public class CMD_Active_心愿瓶_BottleWishInfo extends Command<BottleWishMessage.BottleWishInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, BottleWishMessage.BottleWishInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        BottleWishMessage.BottleWishInfoResponse.Builder response = BottleWishMessage.BottleWishInfoResponse.newBuilder();
        try {
            ActiveModule_心愿瓶 am = ActiveModule_心愿瓶.get(playercode, false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            //todo 许愿瓶信息
            HashMap<Integer, Integer> bottle = am.getBottle();
            Map<String, actwish> allData = DataCenter.getAllData(actwish.class);
            for (actwish value : allData.values()) {
                BottleWishMessage.BottleMsg.Builder builder = BottleWishMessage.BottleMsg.newBuilder();
                builder.setDataId(value.getId());
                builder.setValue(bottle.get(value.getId()));
                builder.setCanRewardValue(value.getConversionScore());
                Currencies curs = new Currencies(Currency.create(value.getRewardtype(), value.getRewardid(), value.getRewardnum()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                builder.addAllItem(items);
                response.addBottles(builder);
            }

            //todo 小垃圾概率信息
            actwishdata data = DataCenter.getData(1, actwishdata.class);
            double totalCounter = 0;//总权重
            for (int i = 0; i < data.getRewardtype().length; i++) {
                if (data.getRewardtype()[i] == 0) continue;
                totalCounter += data.getCounter()[i];
            }

            for (int i = 0; i < data.getRewardtype().length; i++) {
                if (data.getRewardtype()[i] == 0) continue;
                BottleWishMessage.ProbabilityMsg.Builder builder = BottleWishMessage.ProbabilityMsg.newBuilder();
                double prob = data.getCounter()[i] / totalCounter;
                builder.setProbability(prob);
                Currency curs = Currency.create(data.getRewardtype()[i], data.getRewardid()[i], data.getRewardnum()[i]);
                PackageMessage.Item item = new CurrencyHandle().packItem(curs);
                builder.setItem(item);
                response.addProbabilityMsg(builder);
            }

            //todo 大奖日志
            PlayerBean pb = PlayerBean.get(playercode);
            ServerBean sb = ServerBean.get(pb.getServerId());
            response.addAllAwardsInfo(am.getLog(sb.getAreaId()));

            //todo 消耗物品信息
            Currency cost = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum() );//花费
            PackageMessage.Item item = new CurrencyHandle().packItem(cost);
            response.setCostItem(item);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }


    private void send(MessageSource messageSource, BottleWishMessage.BottleWishInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
